package org.scboy.rbac.controller;

import java.util.List;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.OrgResourceService;
import org.scboy.rbac.service.OrgService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("rbac/org")
@Menu("权限管理系统.组织管理")
public class OrgController {
	
	@Autowired
	OrgService orgService;
	
	@Autowired
	OrgResourceService orgResourceService;
	
	/**
	 * 分页查询组织列表
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Permission("分页查询组织列表")
	public Result page(@RequestBody Model params){
		Page page=orgService.page(params);
		return Result.Success(page.getData(),page.getCount());
	}
	
	/**
	 * 获取组织树
	 * @param request
	 * @return
	 */
	@GetMapping("all")
	@Permission("获取所有组织")
	public Result all(String parentId){
		return Result.Success(orgService.getOrgs(parentId),"");
	}
	
	/**
	 * 查询子组织
	 * @param request
	 * @param parentId
	 * @return
	 */
	@PostMapping("childs")
	@Permission("查询子组织")
	public Result childs(@RequestBody Model params){
		List<Model> datas=orgService.getChilds(params);
		return Result.Success(datas,0L+datas.size());
	}
	
	/**
	 * 查询子组织
	 * @param request
	 * @param parentId
	 * @return
	 */
	@GetMapping("tree")
	@Permission("查询组织树")
	public Result tree(Model params){
		List<Model> datas=orgService.tree(params);
		return Result.Success(datas,0L+datas.size());
	}
	
	/**
	 * 新增组织
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增组织")
	public Result add(@RequestBody Model params){
		if(orgService.checkCode(params.getString("orgCode"))){
			return Result.Error("RBAC_ORG_CHECK_CODE");
		}
		if(orgService.add(params)){
			return Result.Success("RBAC_ORG_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_ORG_ADD_ERROR");
		}	
	}
	
	/**
	 * 获取组织信息
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("getTop")
	@Permission("获取当前用户顶级组织")
	public Result getTop(String id){
		return Result.Success(orgService.getTop(id), "");	
	}
	
	/**
	 * 获取组织信息
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取组织信息")
	public Result get(String id){
		return Result.Success(orgService.get(id), "");	
	}
	
	/**
	 * 更新组织
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新组织")
	public Result update(@RequestBody Model params){
		Model oldModel=orgService.get(params.getString("id"));
		if(oldModel==null){
			return Result.Error("RBAC_ORG_UPDATE_ERROR");
		}else if(!oldModel.getString("orgCode").equals(params.getString("orgCode"))){
			if(orgService.checkCode(params.getString("orgCode"))){
				return Result.Error("RBAC_ORG_CHECK_CODE");
			}
		}
		if(orgService.update(params)){
			return Result.Success("RBAC_ORG_UPDATE_SUCCESS");
		}else{
			return Result.Error("RBAC_ORG_UPDATE_ERROR");
		}
	}

	/**
	 * 升序降序
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("sort")
	@Permission("升序降序")
	public Result sort(String id,int type){
		if(orgService.sort(id, type)){
			return Result.Success("RBAC_ORG_SORT_SUCCESS");
		}else{
			return Result.Error("RBAC_ORG_SORT_ERROR");
		}
	}
	
	/**
	 * 合并组织
	 * @param request
	 * @param firstId
	 * @param secondId
	 * @return
	 */
	@PutMapping("merge")
	@Permission("合并组织")
	public Result merge(@RequestBody Model params){
		if(orgService.checkCode(params.getString("orgCode"))){
			return Result.Error("RBAC_ORG_CHECK_CODE");
		}
		if(orgService.merge(params)){
			return Result.Success("RBAC_ORG_MERGE_SUCCESS");
		}else{
			return Result.Error("RBAC_ORG_MERGE_ERROR");
		}
	}
	
	/**
	 * 删除组织
	 * @param request
	 * @param orgId
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除组织")
	public Result delete(String id){
		if(orgService.delete(id)){
			return Result.Success("RBAC_ORG_DELETE_SUCCESS");
		}else{
			return Result.Error("RBAC_ORG_DELETE_ERROR");
		}	
	}
	
	/**
	 * 新增组织资源关系
	 * @param request
	 * @param orgId
	 * @param roleId
	 * @return
	 */
	@PostMapping("addorgresource")
	@Permission("新增组织资源关系")
	public Result addOrgResource(String orgId,String resId){
		if(orgResourceService.add(orgId, resId)){
			return Result.Success("RBAC_ORG_ROLE_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_ORG_ROLE_ADD_ERROR");
		}
	}
	
	/**
	 * 移除组织资源关系
	 * @param request
	 * @param id
	 * @return
	 */
	@DeleteMapping("removeorgrole")
	@Permission("移除组织资源关系")
	public Result removeOrgRole(String id){
		if(orgResourceService.delete(id)){
			return Result.Success("RBAC_ORG_ROLE_REMOVE_SUCCESS");
		}else{
			return Result.Error("RBAC_ORG_ROLE_REMOVE_ERROR");
		}
	}
	
	/**
	 * 分页获取组织相关人员
	 * @param request
	 * @param id
	 * @return
	 */
	@PostMapping("getusers")
	@Permission("分页获取组织相关人员")
	public Result getUsers(@RequestBody Model params){
		Page page=orgService.getUsers(params);
		return Result.Success(page.getData(),page.getCount());
	}
	
	/**
	 * 分页获取组织相关资源
	 * @param request
	 * @param id
	 * @return
	 */
	@PostMapping("getress")
	@Permission("分页获取组织相关资源")
	public Result getRess(@RequestBody Model params){
		Page page=orgResourceService.page(params);
		return Result.Success(page.getData(),page.getCount());
	}

	
	/**
	 * 重构组织信息
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("reinfo")
	@Permission("重构组织信息")
	public Result reinfo(){
		orgService.grenAll("root");
		return Result.Success("RBAC_ORG_REALL_SUCCESS");
	}
}
